package dev.polls.skycube;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class SkyCubeActivity extends Activity {
	GetJSon gjs = new GetJSon();
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		SharedPreferences settings = getSharedPreferences("account_info",0);
		String check_Pass = settings.getString("username", "");
		if(check_Pass!="")
		{
	 		Intent i = new Intent(SkyCubeActivity.this, TabHandler.class);
			startActivity(i);
			finish();
		}
	 	final Button button = (Button) findViewById(R.id.submitButton);
	 	button.setOnClickListener(new View.OnClickListener() //called when login button is touched
	 	{
	 		public void onClick(View arg0) 
	 		{
	 			try
	 			{
	 				//ProgressDialog dialog = ProgressDialog.show(SkyCubeActivity.this,"","logging in, Please wait...", true);
	 				Login();
	 		    }
	 		    catch (Exception e)
	 		    {
	 		    	// TODO Auto-generated catch block
	 				e.printStackTrace();
	 			}
	 		}	 		
	 	});
	 	
    }
	/**
	 * Safe login info to sharedPreferences
	 */
	public void Safe_Pass()	
	{
		final EditText User   = (EditText)findViewById(R.id.hostField);
		final EditText Pass_name = (EditText) findViewById(R.id.passField);
		
		SharedPreferences settings = getSharedPreferences("account_info",0);
		SharedPreferences.Editor editor = settings.edit();
		editor.clear();
		editor.putString("username", User.getText().toString());
		editor.putString("password", Pass_name.getText().toString());
		editor.commit();
	}

	public void Login() throws ClientProtocolException, URISyntaxException, IOException, JSONException
	{
		BufferedReader in = null;
		String output;
		
		final EditText User   = (EditText)findViewById(R.id.hostField); //get field values
		final EditText Pass_name = (EditText) findViewById(R.id.passField);
		String usern = User.getText().toString();
		String passn = Pass_name.getText().toString();
		
		gjs.getGroupList(usern, passn);
		gjs.Json_Values(usern,passn);
		HttpClient client = new DefaultHttpClient(); 
        HttpGet request = new HttpGet();	//define httpget 
        request.setURI(new URI("http://www.skycube.eu/Android/Login?Username="+usern+"&Password="+passn));
        HttpResponse response = client.execute(request);
        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        output=in.readLine();
        System.out.println(output);
	
		if(output.equals("True"))
		{
			Safe_Pass();
			ProgressDialog logging = ProgressDialog.show(SkyCubeActivity.this,"","Logging In, Please wait...", true);
			Intent i = new Intent(SkyCubeActivity.this, TabHandler.class); //forward to the application
			startActivity(i);
			finish();
		}
		else
        {
        	final TextView erv = (TextView) findViewById(R.id.errorView);
        	erv.setText("Login data incorrect");
        }
	}
	
}